package com.nanfang51g3.ckz.ui.update.password;

import android.text.TextUtils;

import com.nanfang51g3.ckz.di.ApiModule;
import com.nanfang51g3.ckz.domain.HttpResultBean;
import com.nanfang51g3.ckz.domain.SuccessBean;
import com.nanfang51g3.ckz.exec.Api2Exception;
import com.nanfang51g3.ckz.http.HttpResultFunc;
import com.nanfang51g3.ckz.http.RestApi;
import com.nanfang51g3.ckz.http.SubscriberInterface;
import com.nanfang51g3.ckz.utils.KLog;
import com.nanfang51g3.ckz.utils.MD5Utils;
import com.nanfang51g3.ckz.utils.SPUtil;

import java.util.TreeMap;

import javax.inject.Inject;

import retrofit2.Retrofit;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/4/13.
 */
public class UpPasswordPresenter implements UpPasswordContract.UpPasswordActionsListener {
    String message;
    private final RestApi _restApi;
    private final UpPasswordContract.View mView;

    @Inject
    public UpPasswordPresenter(RestApi restApi, UpPasswordContract.View mView) {
        KLog.i("TAG", "进入了...UpPasswordPresenter");
        /**
         * @see ApiModule#provideRestApi(Retrofit)
         */
        this._restApi = restApi;
        KLog.i("TAG", _restApi == null ? "进入了...is null" : "进入了...not null");
        /**
         * @see UpPasswordPresenterModule#UpPasswordPresenterModule(UpPasswordContract.View)
         */
        this.mView = mView;
        KLog.i("TAG", mView == null ? "进入了...is null" : "进入了...not null");
    }

    @Override
    public void updatePassword(String oldpass, String password, String confirmPassword) {
        TreeMap<String, String> treeMap = new TreeMap<>();
        String userid = (String) SPUtil.get(mView.getContext(), "userid", "");

        if (TextUtils.isEmpty(oldpass)) {
            mView.showMessage("旧密码不能为空");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            mView.showMessage("新密码不能为空");
            return;
        }
        if (TextUtils.isEmpty(confirmPassword)) {
            mView.showMessage("确认密码不能为空");
            return;
        }
        if (!password.equals(confirmPassword)) {
            mView.showMessage("新密码与确认密码不一致");
            return;
        }
        treeMap.put("userid", userid);
        treeMap.put("oldpass", oldpass);
        treeMap.put("password", password);
        String sign = MD5Utils.getAccessToken(treeMap);
        Observable observable = _restApi.repass(userid, oldpass, password, sign);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new UpPasswordResultFunc<SuccessBean>())
                .subscribe(new SubscriberInterface<SuccessBean>() {
                    @Override
                    public void onCompleted() {
                        KLog.i("Evd", "onCompleted");
                    }

                    @Override
                    public void _onError(String msg) {
                        KLog.i("Evd", "onError " + msg);
                        /**
                         * @see  UpPasswordActivity#showMessage(String)
                         */
                        mView.showMessage(msg);
                    }

                    @Override
                    public void onNext(SuccessBean o) {
                        KLog.i("Evd", "onNext " + o.toString());
                        /**
                         * @see  UpPasswordActivity#exitLogin()
                         */
                        SPUtil.putAndApply(mView.getContext(), "is_login", false);
                        /**
                         * @see UpPasswordActivity#showMessage(String)
                         */
                        mView.showMessage(message);
                        /**
                         * @see UpPasswordActivity#exitLogin()
                         */
                        mView.exitLogin();
                    }
                });
    }

    class UpPasswordResultFunc<T> extends HttpResultFunc<T> {
        @Override
        public T call(HttpResultBean<T> httpResult) {
            KLog.i("Evd", httpResult == null ? "1-" : "0-" + httpResult.getCode() + "-" + httpResult.getInfo() + "-" + httpResult.getData().toString());
            if (httpResult.getCode() == 2 || httpResult.getCode() == 3 || httpResult.getCode() == 4) {
                throw new Api2Exception(httpResult.getInfo());
            }
            message = httpResult.getInfo();
            return httpResult.getData();
        }
    }
}
